home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2681 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  74 lines

  1. Path: news.walrus.com!news
  2. From: fjordao@walrus.com (Felipe Jordao)
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] Dynamically allocating memory for a char*
  5. Date: Fri, 19 Jan 1996 00:00:49 GMT
  6. Organization: HAC
  7. Message-ID: <4dmn1i$10t@walrus2.walrus.com>
  8. NNTP-Posting-Host: p12.ts1.walrus.com
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. Hi,
  12.  
  13. I have a question about allocating memory for a string as it's passed
  14. in by the user.  I'm using cin to get the info, but maybe this is
  15. inappropriate.  I have included the relevant snippets of the file
  16. below.  What I am trying to achieve is no limitation for the user when
  17. they input a path + filename.  Is it possible to use char* foo and
  18. then something like (I know this is wrong, but the idea of it ...)
  19.  
  20. cin >> malloc(sizeof(infile));             :0
  21.  
  22. Here is the actual code
  23. ----------------------------------------------------------------------
  24.  
  25. main()
  26. {
  27.  ifstream InputFile;     // Input file stream
  28.  ofstream OutputFile;    // Output file stream
  29.  .
  30.  .
  31.  .
  32.  OpenFiles(InputFile, OutputFile);
  33.  .
  34.  .
  35. }
  36.  
  37.  
  38. void OpenFiles(ifstream& InputFile, ofstream& OutputFile)
  39. {
  40.  char infile[30];        // <---- RIGHT HERE.  this works as it is, 
  41.  char outfile[30];       //       but I would like to use
  42.                          //       char *infile, *outfile.  When
  43.                          //       I try, I have not found a way to
  44.                          //       dynamically allocate memory when the
  45.                          //       user inputs the file.  I know I can
  46.                          //       use something like infile = new char[X]
  47.                          //       but that still leave me with the X limit.  
  48.  
  49.  cout << "Enter the path and filename of" << endl   
  50.       << "the input file (*.csv): ";                
  51.  cin  >> infile;         // I would like to allocate memory right after 
  52.                          // user inputs the name of the file.  If I use
  53.                          // *infile and leave it like this it crashes
  54.                          // because of unallocated space
  55.  
  56.  InputFile.open(infile);       // Test to see if the input file opens
  57.  if (!InputFile) {
  58.     cerr << "Cannot open input file!" << endl;
  59.     exit(1);
  60.     }
  61.  
  62.  
  63.  cout << "Enter the path and filename of" << endl  // get output file
  64.       << "the output file (*.bcp): ";              // from user
  65.  cin  >> outfile;
  66.  
  67.  OutputFile.open(outfile);    // Test to see if the output file opens
  68.  if (!OutputFile) {
  69.     cerr << "Cannot open output file!" << endl;
  70.     exit(1);
  71.     }
  72. }
  73.  
  74.